package com.platform.modules.sys.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.JedisUtil;
import com.platform.modules.sys.dao.SysPerProcessDao;
import com.platform.modules.sys.entity.SysPerProcessEntity;
import com.platform.modules.sys.service.SysPerProcessService;

/**
 * 流程权限设置类
 *
 * @author 肖亮
 */
@Service("sysPerProcessService")
public class SysPerProcessServiceImpl extends ServiceImpl<SysPerProcessDao, SysPerProcessEntity> implements SysPerProcessService {
	@Autowired
	JedisUtil jedisUtil;

	@Override
	public List<SysPerProcessEntity> queryAll(Map<String, Object> params) {
		return baseMapper.queryAll(params);
	}
	
	@Override
	public List<SysPerProcessEntity> queryList(Map<String, Object> params) {
		return baseMapper.queryAll(params);
	}
	
	@Override
	public List<String> queryPerProcessCodeByRoleId(String roleId) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("roleId", roleId);
		return baseMapper.queryPerProcessCodeByRoleId(params);
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdate(String roleId, List<String> processCodeList,String userId) {
		// 先删除角色与菜单关系
		deleteBatch(new String[] { roleId });

		if (null==processCodeList || processCodeList.size() == 0) {
			return;
		}

		// 保存角色与菜单关系
		List<SysPerProcessEntity> list = new ArrayList<>(processCodeList.size());
		for (String processCode : processCodeList) {
			SysPerProcessEntity sysPerProcessEntity = new SysPerProcessEntity();
			sysPerProcessEntity.setRoleId(roleId);
			sysPerProcessEntity.setProcessCode(processCode);
			sysPerProcessEntity.setCrearteTime(new Date());
			sysPerProcessEntity.setCreateUserId(userId);
			list.add(sysPerProcessEntity);
		}
		this.saveBatch(list);
	}
	
	
	@Override
	public int deleteBatch(String[] roleIds) {
		return baseMapper.deleteBatch(roleIds);
	}
}
